/**
 * Created with JetBrains WebStorm.
 * User: Администратор
 * Date: 9/12/13
 * Time: 8:39 PM
 * To change this template use File | Settings | File Templates.
 */
function CanvasMarker() {
    this.cnv = null;
    var colorPlus, colorMinus, rgba1, rgba2, op;

    this.init = function () {
        this.cnv = [];
    };


    this.open = function (countMarker, speed, azimut, noSpid, signal) {
        this.init();
        this.setCanvas(countMarker,speed,azimut);

    };

    this.setCanvas = function (countMarker,speed,azimut) {
        document.getElementById('markerId' + countMarker).innerHTML = '<canvas width="30" height="30" id="cnvMarkerId' + countMarker + '"></canvas>';
        this.cnv[countMarker] = document.getElementById('cnvMarkerId' + countMarker).getContext('2d');
        this.draw(this.cnv[countMarker],countMarker,speed,azimut);

    };

    this.draw = function (cnv, countMarker,speed,azimut) {
        var timeAgo = this.difference(countMarker);
        cnv.translate(15, 15);
        if(timeAgo<=600){

            if(speed==0){
                cnv.beginPath();
                cnv.arc(1, 2, 7, 0, 2*Math.PI, true);
                var gradient = cnv.createRadialGradient(0, -4, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(0,0,0,1)');
                gradient.addColorStop("0.5",'rgba(0,0,0,0.5)');
                gradient.addColorStop("1.0",'rgba(0,0,0,0.2)');
                cnv.fillStyle = gradient;
                cnv.fill();

                cnv.beginPath();
                cnv.arc(0, 0, 7, 0, 2*Math.PI, true);
                var gradient = cnv.createRadialGradient(0, -4, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(255,255,255,1)');
                gradient.addColorStop("0.5",'rgba(255,93,0,1)');

                gradient.addColorStop("1.0",'rgba(195,93,0,1)');
                cnv.fillStyle = gradient;
                cnv.fill();

                cnv.beginPath();
                cnv.arc(0, 0, 7, 0, 2*Math.PI, true);
                cnv.strokeStyle = '#9b4d00';
                cnv.stroke();
            }else{

                cnv.rotate(this.degree(azimut));

                cnv.beginPath();
                cnv.moveTo(-8, 10);
                cnv.lineTo(0, -11);
                cnv.lineTo(8, 11);
                cnv.lineTo(0, 5);
                cnv.lineTo(-8, 11);
                var gradient = cnv.createRadialGradient(0, -2, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(0,0,0,1)');
                gradient.addColorStop("0.5",'rgba(0,0,0,0.5)');
                gradient.addColorStop("1.0",'rgba(0,0,0,0.4)');
                cnv.fillStyle = gradient;
                cnv.fill();



                cnv.beginPath();
                cnv.moveTo(-7, 9);
                cnv.lineTo(0, -9);
                cnv.lineTo(7, 9);
                cnv.lineTo(0, 3);
                cnv.lineTo(-7, 9);
                var gradient = cnv.createRadialGradient(0, -2, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(255,255,255,1)');
                gradient.addColorStop("0.5",'rgba(0,96,255,1)');
                gradient.addColorStop("1.0",'rgba(0,0,255,1)');
                cnv.fillStyle = gradient;
                cnv.fill();




               cnv.lineWidth = 1;
                cnv.strokeStyle = '#00000';
                cnv.stroke();
              //  alert(azimut)
            }



           // alert('ds');

        }else if(timeAgo<86400){

            colorPlus = parseFloat((255 - (timeAgo*(255/86400))).toFixed(0));
            colorMinus = parseFloat((timeAgo*(255/86400)).toFixed(0));
            op = 1- timeAgo/86400;
            rgba1 = 'rgba(255,255'+','+colorMinus+','+'1'+')';
            rgba2 = 'rgba('+colorPlus+','+colorPlus+','+colorPlus+',1)';
            var gradient = cnv.createRadialGradient(0, 0, 0, 0, 0, 14);
            gradient.addColorStop("0",rgba1);
            gradient.addColorStop("1.0",'rgba(255,255,255,0.5)');
            cnv.fillStyle = gradient;

            cnv.fillRect(-7, -7, 14, 14);
            cnv.beginPath();
            cnv.moveTo(-7, -7);
            cnv.lineTo(7, -7);
            cnv.lineTo(7, 7);
            cnv.lineTo(-7, 7);
            cnv.lineTo(-7, -7);
            cnv.lineWidth = 1;
            cnv.strokeStyle = '#00000';
            cnv.stroke();
            //alert('86400');
        }else{
            var gradient = cnv.createRadialGradient(0, 0, 0, 0, 0, 14);
            // Add the colors with fixed stops at 1/4 of the width.
            gradient.addColorStop("0",'rgba(255,255,255,1)');
            gradient.addColorStop("1.0",'rgba(255,255,255,0.5)');
            // Use the gradient as a fill pattern
            cnv.fillStyle = gradient;

            cnv.fillRect(-7, -7, 14, 14);

            cnv.beginPath();
            cnv.moveTo(-7, -7);
            cnv.lineTo(7, -7);
            cnv.lineTo(7, 7);
            cnv.lineTo(-7, 7);
            cnv.lineTo(-7, -7);
            cnv.lineWidth = 1;

            // set line color
            cnv.strokeStyle = 'rgba(0,0,0,1)';
            cnv.stroke();
        }

    };

    this.difference = function (i) {
        var presentSS = calcTime.ssYear(calcTime.present_time());//секунд с начала года текущие
        var parcelSS = calcTime.ssYear(calcTime.plusGmt(arrLastPos[i][3]));  //секунд с начала года в посылке
        return (presentSS - parcelSS);
    };

    this.degree = function(a) {
        return a * Math.PI / 180;
    };

}
var canvasMarker = new CanvasMarker;
